//
//  HWSegmentedBgTitleDataSource.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/27.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWSegmentedBgTitleDataSource: JXSegmentedTitleDataSource {
    open override func preferredItemModelInstance() -> JXSegmentedBaseItemModel {
        return HWSegmentedBgTitleItemModel()
    }

    open override func preferredRefreshItemModel(_ itemModel: JXSegmentedBaseItemModel, at index: Int, selectedIndex: Int) {
        super.preferredRefreshItemModel(itemModel, at: index, selectedIndex: selectedIndex)

        guard let itemModel = itemModel as? HWSegmentedBgTitleItemModel else {
            return
        }

//        itemModel.titleGradientStartPoint = titleGradientStartPoint
//        itemModel.titleGradientEndPoint = titleGradientEndPoint
//        itemModel.titleNormalGradientColors = titleNormalGradientColors
//        itemModel.titleSelectedGradientColors = titleSelectedGradientColors
//        if index == selectedIndex {
//            itemModel.titleCurrentGradientColors = itemModel.titleSelectedGradientColors
//        }else {
//            itemModel.titleCurrentGradientColors = itemModel.titleNormalGradientColors
//        }
    }

    //MARK: - JXSegmentedViewDataSource
    open override func registerCellClass(in segmentedView: JXSegmentedView) {
        segmentedView.collectionView.register(HWSegmentedBgTitleCell.self, forCellWithReuseIdentifier: "cell")
    }

    open override func segmentedView(_ segmentedView: JXSegmentedView, cellForItemAt index: Int) -> JXSegmentedBaseCell {
        let cell = segmentedView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
        return cell
    }

    open override func refreshItemModel(_ segmentedView: JXSegmentedView, leftItemModel: JXSegmentedBaseItemModel, rightItemModel: JXSegmentedBaseItemModel, percent: CGFloat) {
        super.refreshItemModel(segmentedView, leftItemModel: leftItemModel, rightItemModel: rightItemModel, percent: percent)

        guard let leftModel = leftItemModel as? HWSegmentedBgTitleItemModel, let rightModel = rightItemModel as? JXSegmentedTitleGradientItemModel else {
            return
        }

//        if isTitleColorGradientEnabled && isItemTransitionEnabled {
//            leftModel.titleCurrentGradientColors = JXSegmentedViewTool.interpolateColors(from: leftModel.titleSelectedGradientColors, to: leftModel.titleNormalGradientColors, percent: percent)
//            rightModel.titleCurrentGradientColors = JXSegmentedViewTool.interpolateColors(from: rightModel.titleNormalGradientColors, to: rightModel.titleSelectedGradientColors, percent: percent)
//        }
    }

    open override func refreshItemModel(_ segmentedView: JXSegmentedView, currentSelectedItemModel: JXSegmentedBaseItemModel, willSelectedItemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
        super.refreshItemModel(segmentedView, currentSelectedItemModel: currentSelectedItemModel, willSelectedItemModel: willSelectedItemModel, selectedType: selectedType)

        guard let myCurrentSelectedItemModel = currentSelectedItemModel as? JXSegmentedTitleGradientItemModel, let myWillSelectedItemModel = willSelectedItemModel as? JXSegmentedTitleGradientItemModel else {
            return
        }

        myCurrentSelectedItemModel.titleCurrentGradientColors = myCurrentSelectedItemModel.titleNormalGradientColors
        myWillSelectedItemModel.titleCurrentGradientColors = myWillSelectedItemModel.titleSelectedGradientColors
    }

}
